package com.jj.message.battle;

import com.jj.message.IoMessage;
import com.jj.message.ProtoFormat;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class UserMessage extends BattleMsg implements IoMessage {
    private static Logger logger = LoggerFactory.getLogger(UserMessage.class);

    private short msgID;
    private ProtoFormat protocolFormat = null;
    private Integer requestId = null;


    public Integer getRequestId() {
        return requestId;
    }

    public void setRequestId(Integer requestId) {
        this.requestId = requestId;
    }

    @Override
    public short getMsgID() {
        return msgID;
    }

    @Override
    public void setMsgID(short msgID) {
        this.msgID = msgID;
    }

    @Override
    public ProtoFormat getProtocolFormat() {
        return protocolFormat;
    }

    @Override
    public void setProtocolFormat(ProtoFormat protocolFormat) {
        this.protocolFormat = protocolFormat;
    }

    @Override
    public byte[] getPayload() {
        return getData();
    }

    @Override
    public void setPayload(byte[] payload) {
        setData(payload);
    }

    @Override
    public String getJsonMsg() {
        return getJsonStr();
    }

    @Override
    public void setJsonMsg(String jsonMsg) {
        setJsonStr(jsonMsg);
    }

    private String payload2String(ProtoFormat format, byte[] data){
        switch (format){
            case ProtoBuf:
            case Standard:
            default:
                return Arrays.toString(data);
            case JSON:
                if(data != null)
                {
                    return new String(data);
                }else{
                    return null;
                }
        }
    }

    @Override
    public String toString() {
        return "UserMessage{" +
                "msgID=" + msgID +
                ", protocolFormat=" + protocolFormat +
                ", payload=" + payload2String(protocolFormat, getData()) +
                '}';
    }

    @Override
    public void write2ByteBuf(ByteBuf byteBuf){
        byteBuf.writeShort(msgID);
        byteBuf.writeByte(protocolFormat.getValue());
        if(requestId != null){
            byteBuf.writeInt(requestId);
        }
        if(getData() != null){
            byteBuf.writeBytes(getData());
        }
    }

    public static UserMessage parseFrom(ByteBuf byteBuf){
        UserMessage userMsg = new UserMessage();

        userMsg.setMsgID(byteBuf.readShort());
        userMsg.setProtocolFormat(ProtoFormat.findByValue(byteBuf.readByte()));
        if(byteBuf.readableBytes() != 0){
            switch (userMsg.protocolFormat){
                case ProtoBuf:
                case Standard:
                case JSON:
                    {
                        byte[] payload = new byte[byteBuf.readableBytes()];
                        byteBuf.readBytes(payload);
                        userMsg.setPayload(payload);
                        break;
                    }
                case Request:
                case Response:
                    {
                        userMsg.setRequestId(byteBuf.readInt());
                        byte[] payload = new byte[byteBuf.readableBytes()];
                        byteBuf.readBytes(payload);
                        userMsg.setPayload(payload);
                        break;
                    }
                case Heartbeat:
                {
                    userMsg.setProtocolFormat(ProtoFormat.Heartbeat);
                }
                default:
                    logger.error("unknown msg type:{}", userMsg.protocolFormat);
                    return null;
            }
        }else{
            logger.debug("empty PB msg for {}", userMsg.getMsgID());
        }
        return userMsg;
    }

}
